<?php
	include('config.php');
	
	$query = 'SELECT U.username, C.comment_time, C.comment_text, T.topic FROM COMMENTS C JOIN THREADS T JOIN USERS U WHERE T.thread_id = C.thread_id AND C.thread_id = ' . $id . ' AND C.user_id = U.user_id ORDER BY comment_time ASC';
			
	$results = mysql_query($query);
	$count = 0;
	
	while($row = mysql_fetch_array($results)) {
		if($count == 0){
			echo '<h2>Viewing Thread:  ' . $row['topic'] . '</h3>';
		}
		
		$text = stripslashes($row['comment_text']);
		if(stristr($text, '[java]') == false && stristr($text, '[xml]') == false){
			$text = nl2br($text);
		}else{			
			if(stristr($text, '[java]') != false){
				if(stristr($text, '[/java]') == false){
					$text = $text . '[/java]';
				}
				
				$before = nl2br(substr($text, 0, strpos($text, '[java]')));
				$after = nl2br(substr($text, strpos($text, '[/java]') + 7, strlen($text) - strpos($text, '[/java]') + 7));;
				$text = substr($text, strpos($text, '[java]'), strpos($text, '[/java]') - strpos($text, '[java]') + 7);
				
				$text = str_replace('[java]', '<pre class="brush:java; class-name: \'code\'">', $text);
				$text = str_replace('[/java]', '</pre>', $text);
				
				$text = $before . $text . $after;
			}
			
			if(stristr($text, '[xml]') != false){
				if(stristr($text, '[/xml]') == false){
					$text = $text . '[/xml]';
				}
				
				$before = nl2br(substr($text, 0, strpos($text, '[xml]')));
				$after = nl2br(substr($text, strpos($text, '[/xml]') + 7, strlen($text) - strpos($text, '[/xml]') + 7));;
				$text = substr($text, strpos($text, '[xml]'), strpos($text, '[/xml]') - strpos($text, '[xml]') + 7);
				
				$text = str_replace('[xml]', '<pre class="brush:xml; class-name: \'code\'">', $text);
				$text = str_replace('[/xml]', '</pre>', $text);
				
				$text = $before . $text . $after;
			}
		}
		
		echo '<div id="comment">' . $text . "<br /><br /><b>" . 
			stripslashes($row['username']) . ' @ ' . stripslashes($row['comment_time']) . '</b><br /></div>';
		
		$count ++;
	}

	echo '</table><br /><br />';
	
	if(isset($_COOKIE['session_id']) && isset($_COOKIE['user_id'])){
		echo '<br /><br /><form action="comment.php?id=' . $id . '" method="POST"><textarea name="comment" cols=84 rows=6></textarea><br />' .
			'<input type="submit" value="Comment"></form>';
		
		echo '<br /><br /><br />';
		echo '**<b>use [java][/java] in order to format Java code an use [xml][/xml] in order to format xml</b>';
	}
	
	mysql_close($mysql_handle);
?>